<?php

namespace app\admin\controller;

use think\Controller;
use app\common\model\Categorys as M;
use think\Model;

class Categorys extends Controller {
    /*
     * 列表页
     * 
     */

    public function index() {
        return $this->fetch();
    }

    /*
     * 列表页获取josn数据
     * 
     */

    public function get_josn() {
        $m = new M();
        $rs = $m->getAll();

        $lists = ['code' => 0, 'msg' => '', 'data' => []];
        foreach ($rs as $k => $v) {
            if (strlen($v['id']) == 2) {
                $v['tpl'] = category_type($v['tpl']);
                $lists['data'][] = $v;
                foreach ($rs as $val) {
                    if (strlen($val['id']) == 2) {
                        continue;
                    }
                    //子级目录
                    if (strlen($val['id']) > 2 && substr($val['id'], 0, 2) == $v['id']) {
                        $space = "";
                        for ($i = strlen($val['id']) / 2; $i > 1; $i--) {
                            $space.="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                        }
                        $space.= "|--";
                        $val['tpl'] = category_type($val['tpl']);
                        $val['name'] = $space . $val['name'];
                        $lists['data'][] = $val;
                    }
                }
            }
        }
        return $lists;
    }

    /*
     * 分类添加页面
     * 
     */

    public function add() {
        $Cate = new M();
        $rs = $Cate->getAll();
        $this->assign('rs', $rs);
        return $this->fetch();
    }

    /*
     * 分类修改页面
     * 
     */

    public function edit() {
        $m = new M();
        $menus = $m->getAll();
        $pid = 0;
        $s = $m->getById(input('id'));
        if (strlen($s->id) != 2) {
            $pid = substr($s->id, 0, -2);
        }
        $this->assign('rs', $menus); //分类列表
        $this->assign('id', input('id'));
        $this->assign('s', $s); //分类详情
        $this->assign('pid', $pid); //上级分类id

        return $this->fetch();
    }

    /*
     * 分类提交数据
     * 
     */

    public function category_submit() {
        $m = new M();
        $pid = $m->get_id(input('post.pid')); //id结构
        if (input('id', 0) == 0) {//添加
            $opt = [
                'id' => $pid,
                'name' => input('name'),
                'alias' => input('alias'),
                'description' => input('description'),
                'keyword' => input('keyword'),
                'sort' => input('sort'),
                'tpl' => input('tpl'),
                'status' => input('status'),
                'pic' => input('pic'),
                'date' => date('Y-m-d'),
                'time' => date('Y-m-d H:i:s'),
            ];
            $rs = $m->add($opt);
            if ($rs[2]) {
                $m->node_update($rs[2]); //修改node
            }
            return js_return($rs[0], $rs[1]);
        } else {//修改
            $opt = [
                'name' => input('name'),
                'alias' => input('alias'),
                'description' => input('description'),
                'keyword' => input('keyword'),
                'sort' => input('sort'),
                'tpl' => input('tpl'),
                'status' => input('status'),
                'pic' => input('pic'),
            ];
            $rs = $m->modify($opt, input('id'));
            return js_return($rs[0], $rs[1]);
        }
    }

    /*
     * 删除
     * 
     */

    public function del_submit() {
        $m = new M();
        $result = $m->destroy(input('id'));
        $m->node_update(input('id'));
        if (false !== $result) {
            return js_return(1, "删除成功");
        } else {
            return js_return(0, $this->getError());
        }
    }

}
